package com.ruoyi.fun.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.fun.domain.EcAnnouncement;
import com.ruoyi.fun.service.IEcAnnouncementService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 移动端公告Controller（无需权限验证）
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@Anonymous
@RestController
@RequestMapping("/mobile/announcement")
public class EcMobileAnnouncementController extends BaseController
{
    @Autowired
    private IEcAnnouncementService ecAnnouncementService;

    /**
     * 查询公告列表（按ORG_ID过滤）
     */
    @GetMapping("/list")
    public TableDataInfo list(EcAnnouncement ecAnnouncement)
    {
        startPage();
        // 移动端从请求参数获取orgId，如果没有则使用默认值103
        if (ecAnnouncement.getOrgId() == null) {
            ecAnnouncement.setOrgId(103L);
        }
        List<EcAnnouncement> list = ecAnnouncementService.selectEcAnnouncementList(ecAnnouncement);
        return getDataTable(list);
    }

    /**
     * 获取公告详细信息
     */
    @GetMapping(value = "/{announcementId}")
    public AjaxResult getInfo(@PathVariable("announcementId") Long announcementId, Long orgId)
    {
        // 移动端从请求参数获取orgId，如果没有则使用默认值103
        Long currentOrgId = (orgId != null) ? orgId : 103L;
        System.out.println("[DEBUG] EcMobileAnnouncementController.getInfo - 公告ID: " + announcementId + ", 当前组织ID: " + currentOrgId);
        
        // 使用带orgId过滤的方法，确保只能获取当前组织的公告
        EcAnnouncement announcement = ecAnnouncementService.selectEcAnnouncementByAnnouncementIdAndOrgId(announcementId, currentOrgId);
        
        System.out.println("[DEBUG] EcMobileAnnouncementController.getInfo - 查询结果: " + (announcement != null ? "找到公告" : "未找到公告"));
        if (announcement != null) {
            System.out.println("[DEBUG] EcMobileAnnouncementController.getInfo - 公告标题: " + announcement.getTitle() + ", 公告组织ID: " + announcement.getOrgId());
        }
        
        // 验证公告是否存在
        if (announcement == null) {
            return AjaxResult.error("公告不存在或无权限访问");
        }
        return AjaxResult.success(announcement);
    }

    /**
     * 获取有效的公告列表（状态为启用的公告）
     */
    @GetMapping("/active")
    public AjaxResult getActiveAnnouncements(EcAnnouncement ecAnnouncement)
    {
        // 移动端从请求参数获取orgId，如果没有则使用默认值103
        if (ecAnnouncement.getOrgId() == null) {
            ecAnnouncement.setOrgId(103L);
        }
        ecAnnouncement.setStatus("0"); // 假设0表示启用状态
        List<EcAnnouncement> list = ecAnnouncementService.selectEcAnnouncementList(ecAnnouncement);
        return AjaxResult.success(list);
    }
}